package com.jewelry.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jewelry.common.util.Constants;
import com.jewelry.dao.IConfigAttrDAO;
import com.jewelry.service.IConfigAttrService;
import com.jewelry.service.IConsumablesService;
import com.jewelry.vo.ConfigAttrVO;
import com.jewelry.vo.ConfigurationVO;
import com.jewelry.vo.ConsumablesVO;

public class ConfigAttrServiceImpl implements IConfigAttrService {
	
	private static Logger logger = LoggerFactory.getLogger(ConfigAttrServiceImpl.class);
	
	private IConfigAttrDAO configAttrDAO;
	
	public IConsumablesService getConsumablesService() {
		return consumablesService;
	}

	public void setConsumablesService(IConsumablesService consumablesService) {
		this.consumablesService = consumablesService;
	}

	private IConsumablesService consumablesService; 

	public IConfigAttrDAO getConfigAttrDAO() {
		return configAttrDAO;
	}

	public void setConfigAttrDAO(IConfigAttrDAO configAttrDAO) {
		this.configAttrDAO = configAttrDAO;
	}
	
	//===================================================================
	
	public int getAttrCountByConfigid(int configid){
		return configAttrDAO.getAttrCountByConfigid(configid);
	}
	
	
	public List<ConfigAttrVO> getConfigAttrListByConfigGroup(ConfigurationVO vo){
		return getConfigAttrListByConfigGroup(vo.getConfigid());
	}
	
	public List<ConfigAttrVO> getConfigAttrListByConfigGroup(int configid){
		 return configAttrDAO.getConfigAttrListByConfigGroup(configid);
	}
	
	public Map<Integer,String> getConfigAttrMapByConfigGroup(int configid){
		if(logger.isDebugEnabled()){
			logger.debug("start getConfigAttrMapByConfigGroup ...");
		}
		
		List<ConfigAttrVO> list = configAttrDAO.getConfigAttrListByConfigGroup(configid);
		Map<Integer,String> map = new HashMap<Integer,String>();
		for(ConfigAttrVO vo:list){
			map.put(vo.getAttrid(),vo.getAttrName());
		}
		return map;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void addConfigAttr(ConfigAttrVO vo) {
		configAttrDAO.addConfigAttr(vo);
		//根据类型需要增加耗材表数据
		if(vo.getConfigid()==Constants.CONSUMABLES_TYPE_COONFIG){
			ConsumablesVO consumablesVO = new ConsumablesVO();
			consumablesVO.setConsumablesid(vo.getAttrid());
			consumablesVO.setConsumablesName(vo.getAttrName());
			consumablesVO.setUnitName(vo.getRemark());
			consumablesVO.setQuantity(0);
			consumablesService.addConsumables(consumablesVO);
		}
	}

	@Override
	public void deleteConfigAttr(int attrid) {
		configAttrDAO.deleteConfigAttr(attrid);
		
	}

	@Override
	public String getSelectTagString(String name,String id,String cssClass,List<ConfigAttrVO> list){
		StringBuilder sb = new StringBuilder();
		sb.append("<select name='").append(name).append("' id='").append(id).append("' class='").append(cssClass).append("'>");
		for(ConfigAttrVO configAttrVO: list){
			sb.append("<option value='").append(configAttrVO.getAttrName()).append("'>").append(configAttrVO.getAttrName()).append("</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}

}
